package com.zlp.mall.portal.security;


import com.zlp.mall.portal.modules.ums.model.Member;
import lombok.experimental.UtilityClass;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;


/**
 * 安全工具类
 *
 */
@UtilityClass
public class SecurityUtils {


	/**
	 * 获取Authentication
	 */
	public Authentication getAuthentication() {
		return SecurityContextHolder.getContext().getAuthentication();
	}

	/**
	 * 获取用户
	 * @param authentication
	 * @return
	 */
	public Member getUser(Authentication authentication) {
		Object principal = authentication.getPrincipal();
		if (principal instanceof Member) {
			return (Member) principal;
		}
		return null;
	}

	/**
	 * 获取用户
	 */
	public Member getUser() {

		Authentication authentication = getAuthentication();
		return getUser(authentication);
	}
}
